<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增活动信息')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-activity-add">
            <h1>基本信息</h1>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">活动名称：</label>
                    <div class="col-sm-7">
                        <input name="activityName" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">活动类型：</label>
                    <div class="col-sm-7">
                        <div class="col-sm-7">
                            <select class="form-control m-b" name="typeId">
                                <option th:each="type:${typeList}" th:value="${type.typeId}" th:text="${type.typeName}" th:selected="${type.typeId}==*{typeId}"></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">联系人：</label>
                    <div class="col-sm-7">
                        <input name="activityContact" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">联系电话：</label>
                    <div class="col-sm-7">
                        <input name="activityPhone" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">报名时间：</label>
                <div class="col-sm-5">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="enrollBegin" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
                <div class="col-sm-5">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="enrollEnd" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">活动时间：</label>
                <div class="col-sm-5">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="activityBegin" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
                <div class="col-sm-5">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="activityEnd" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">服务时长：</label>
                <div class="col-sm-7">
                    <input name="serveTime" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">活动区域：</label>
                <div class="col-sm-7">
                    <input name="activityArea" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">活动详情：</label>
                <div class="col-sm-7">
                    <textarea name="activityDetail" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">列表展示图片：</label>
                <div class="col-sm-7">
                    <input type="file" id="file" name="activityCover"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">参与的志愿顾问：</label>
                <div class="col-sm-7">
                    <select class="form-control m-b" name="adviserId">
                        <option th:each="adviser:${advisers}" th:value="${adviser.adviserId}" th:text="${adviser.adviserName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">是否需要报名：</label>
                <div class="col-sm-7">
                    <label><input type="radio" name="isEnroll" value="1">是</label>
                    <label><input type="radio" name="isEnroll" value="0">否</label>
                </div>
            </div>
            <div class="clearfix">
                <h1 style="float:left;">岗位信息</h1>
                <button type="button" onclick="addPost()" class="btn btn-success">添加岗位</button>
            </div>
            <div id="post"></div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        let postNum = 0;
        var prefix = ctx + "vtsp/publish"
        $("#form-activity-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            let o=$('#form-activity-add').serializeObject()
            let posts=[]
            for (let i = 0; i < postNum; i++) {
                let postName=document.getElementsByClassName("postName")[i].value;
                let postNum=document.getElementsByClassName("postNum")[i].value;
                let postDetail=document.getElementsByClassName("postDetail")[i].value;
                posts.push({
                    postNum,postName,postDetail
                })
            }
            o.posts=posts
            if ($.validate.form()) {
                $.ajax({
                    url:prefix + "/add",
                    type:"POST",
                    data:JSON.stringify(o),
                    contentType:"application/json",  //缺失会出现URL编码，无法转成json对象
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
            }
        }

        $.fn.serializeObject = function() {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [ o[this.name] ];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
        function addPost() {
            $("#post").append($("#postTpl").text())
            postNum++;
        }

        $("input[name='enrollBegin']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='enrollEnd']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='activityBegin']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='activityEnd']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
<script id="postTpl" type="text/template">
    <div class="form-group">
        <label class="col-sm-2 control-label">岗位名称：</label>
        <div class="col-sm-7">
            <input name="postName" class="form-control postName" type="text" required>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">岗位人数：</label>
        <div class="col-sm-7">
            <input name="postNum" class="form-control postNum" type="text" required>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">岗位描述：</label>
        <div class="col-sm-7">
            <textarea name="postDetail" class="form-control postDetail"></textarea>
        </div>
    </div>
</script>
</html>
